<?php
// index.php?mod=home&childmod=home&ajax=true&op=file_updater_insert
$op = get_get("op");
if ($op == "") {
    returnMessage("-1", "unknownrequest");
}
if ($op == "loadAjaxImageUpdater") {
    loadTemp("system/image_updater.php");
    die();
}
if ($op == "general_image_update") {
    $data = onFileUpdate($_FILES["file"]["name"], $_FILES["file"]["tmp_name"], $_FILES["file"]["size"]);
    if ($data) {
        returnMessage("0", "成功", $data);
    } else {
        returnMessage("-18", "数据传输失败，数据库上传失败，请联系管理员");
    }
}
if ($op == "publishactivity") {
    if (! hasPermission("activity_publish")) {
        returnMessage(- 1, "无权发布动态");
        die();
    }
    $subject = get_post("subject");
    if (! $subject) {
        returnMessage(- 2, "标题不能为空");
        die();
    }
    $content = get_post("content");
    if (! $content) {
        returnMessage(- 3, "内容不能为空");
        die();
    }
    $view_level = get_post("view_level");
    $images = get_post("images");
    $aid = sendActivity($subject, $content, $images, $view_level);
    returnMessage(0, "发送成功", array(
        "aid" => $aid
    ));
}
if ($op == "activity") {
    $aid = get_get("aid");
    if (! $aid) {
        printMessage("error", "请求无效，无效的aid");
        die();
    }
    $data = getActivity($aid);
    if (! $data) {
        printMessage("error", "目标未找到 $aid");
        die();
    }
    $uid = $data["uid"];
    $s_uid = getSessionUid();
    $admin = false;
    if ($uid == $s_uid) {
        $admin = true;
    } else {
        if (isAdmin($s_uid, $uid)) {
            $admin = true;
        }
    }
    if (! allowVisit($s_uid, $uid)) {
        printMessage("error", "你无权访问对方的动态");
        die();
    }
    $view_level = $data["view_level"];

    if (! $admin && $view_level == 1) {
        printMessage("error", "你无权访问对方的私有动态");
        die();
    }
    $appendixes = array();
    if (is_array($data["appendixes"]) != 0) {
        foreach ($data["appendixes"] as $v) {
            $appendixes[$v] = getResourcePathByRid($v);
        }
    }
    $autherdata = getCardInfo($uid);
    $allowCommit = ($uid == $s_uid || allowComment($uid));

    $setting = array(
        "author" => $autherdata,
        "data" => $data,
        "admin" => $admin,
        "appendixes" => $appendixes,
        "allowcomment" => $allowCommit,
        "comments" => getCommentTree($aid)
    );

    setSetting("view_activity_detial", $setting);
    loadTemp("home/zone_view/view_activity_detial.php");
    die();
}
if ($op == "activity_load_more") {
    $uid = get_post("uid");
    $offset = get_post("offset");
    if ($offset < 0) {
        $offset = 0;
    }
    if ($uid != getSessionUid()) {
        if (! allowVisit(getSessionUid(), $uid)) {
            returnMessage(- 5, "您没有足够的权限来访问");
        }
    }
    $data = array();

    $activities = listActivities($uid, $offset, 5, getSessionUid());
    if (sizeof($activities) < 3) {
        $data["bottom"] = true;
    }
    $data["amount"] = sizeof($activities);
    $data["close"] = true;
    $data["data"] = array_reverse($activities);
    $data["op"] = "load_more";
    returnMessage(0, "请求成功", $data);
}

if ($op == "activity_load_more_class") {
    $classid = get_post("classid");
    $offset = get_post("offset");
    if ($offset < 0) {
        $offset = 0;
    }
    $data = array();
    $activities = listActivitiesByClassid($classid, $offset, 5, getSessionUid());
    if (sizeof($activities) < 3) {
        $data["bottom"] = true;
    }
    $data["amount"] = sizeof($activities);
    $data["close"] = true;
    $data["data"] = array_reverse($activities);
    $data["op"] = "load_more";
    returnMessage(0, "请求成功", $data);
}

if ($op == "activity_operate") {
    $s_op = get_post("op");
    if (! $s_op) {
        returnMessage(- 1, "无效的请求:" . json_encode($_POST));
    }
    if ($s_op == "removeComment") {
        $cid = get_post("cid");
        if (! $cid)
            returnMessage(- 2, "无效的请求");
        $data = getCommentData($cid);
        if (! $data)
            returnMessage(- 3, "目标不存在，无法删除");
        $content = $data["content"];
        $aid = $data["aid"];
        $activity = getActivity($aid);
        if (! $activity)
            returnMessage(- 4, "未知错误，无法追溯到动态，无法删除，请联系管理员： 动态id：$aid,评论id:$cid");
        $s_uid = getSessionUid();
        $uid = $activity["uid"];
        if (! ($s_uid == $uid || isAdmin($s_uid, $uid)))
            returnMessage(- 5, "您没有足够的权限来操作这个");
        $back = array(
            "op" => "request",
            "close" => true,
            "url" => "index.php?mod=home&childmod=home&ajax=true&op=activity&aid=$aid"
        );
        if (removeComment($cid) == - 1)
            returnMessage(- 3, "目标不存在，无法删除");
        else {
            $info = substr($content, 0, 10);
            if ($s_uid != $uid)
                sendNotice($uid, "你的回复“ $info ”被 {sender} 删除了", $s_uid);
            returnMessage(0, "删除成功", $back);
        }
    }
    if ($s_op == "removeActivity") {
        $aid = get_post("aid");
        if (! $aid)
            returnMessage(- 2, "无效的请求");
        $activity = getActivity($aid);

        if (! $activity)
            returnMessage(- 4, "未知错误，无法追溯到动态，无法删除，请联系管理员： 动态id：$aid");
        $subject = $activity["subject"];
        $s_uid = getSessionUid();
        $uid = $activity["uid"];
        if (! ($s_uid == $uid || isAdmin($s_uid, $uid)))
            returnMessage(- 5, "您没有足够的权限来操作这个");
        removeActivity($aid);
        $back = array(
            "op" => "request",
            "url" => "index.php?mod=home&childmod=home&ajax=true&op=view_activity_list"
        );
        if ($s_uid != $uid)
            sendNotice($uid, "你的动态“ $subject ”被 {sender} 删除了", $s_uid);
        returnMessage(0, "删除成功", $back);
    }
    if ($s_op == "removeActivityFormList") {
        $aid = get_post("aid");
        if (! $aid)
            returnMessage(- 2, "无效的请求");
        $activity = getActivity($aid);
        if (! $activity)
            returnMessage(- 4, "未知错误，无法追溯到动态，无法删除，请联系管理员： 动态id：$aid");
        $subject = $activity["subject"];
        ;
        $s_uid = getSessionUid();
        $uid = $activity["uid"];
        if (! ($s_uid == $uid || isAdmin($s_uid, $uid)))
            returnMessage(- 5, "您没有足够的权限来操作这个");
        removeActivity($aid);
        $back = array(
            "op" => "remove",
            "aid" => $aid
        );
        if ($s_uid != $uid)
            sendNotice($uid, "你的动态“ $subject ”被 {sender} 删除了", $s_uid);
        returnMessage(0, "删除成功", $back);
    }

    returnMessage("-1", "无效的请求");
}
if ($op == "view_activity_send") {
    if (! hasPermission("activity_publish")) {
        printMessage("error", "请求无效，你没有权限发布动态");
        die();
    }
    loadTemp("home/zone_view/activity_send.php");
    die();
}
if ($op == "replyactivity") {
    $aid = get_post("aid");
    if (! $aid)
        returnMessage(- 1, "无效的动态id $aid");
    $content = get_post("content");
    if (! $content)
        returnMessage(- 2, "内容不能为空");
    $activity = getActivity($aid);
    if (! $activity)
        returnMessage(- 3, "无法找到动态");
    $subject = $activity["subject"];
    $back = array(
        "op" => "request",
        "url" => "index.php?mod=home&childmod=home&ajax=true&op=activity&aid=$aid"
    );
    $uid = $activity["uid"];
    $s_uid = getSessionUid();
    if (! ($uid == $s_uid || allowComment($uid)))
        returnMessage(- 4, "对方设定了禁止评论或你无权评论");
    $cid = get_post("cid");
    if (! $cid) {
        if (sendComment($content, $aid)) {
            if ($s_uid != $uid)
                sendNotice($uid, "你的动态“<a href=index.php?mod=home&childmod=zone&op=activity&aid=$aid> $subject </a>”被 {sender} 评论了<br>$content", $s_uid);
            returnMessage(0, "评论成功", $back);
        } else {
            returnMessage(- 10, "数据库错误，请联系管理员");
        }
    } else {
        $parent_cid = get_post("parent_cid");
        if (! $parent_cid) {
            $parent_cid = $cid;
        }
        $parent = getCommentData($parent_cid);
        if (! $parent) {
            returnMessage(- 6, "无法找到父评论");
        }
        $parentuid = $parent["uid"];
        if (sendComment($content, $aid, $s_uid, $parent_cid, $cid)) {
            if ($s_uid != $uid)
                sendNotice($uid, "你在“<a href=index.php?mod=home&childmod=zone&op=activity&aid=$aid> $subject </a>”的评论，被 {sender} 回复了：<br>$content", $s_uid);
            if ($s_uid != $parentuid)
                sendNotice($parentuid, "你在“<a href=index.php?mod=home&childmod=zone&op=activity&aid=$aid> $subject </a>”的评论，被 {sender} 回复了：<br>$content", $s_uid);
            $back["close"] = false;

            returnMessage(0, "评论成功$parentuid", $back);
        } else {
            returnMessage(- 10, "数据库错误，请联系管理员");
        }
    }
}
if ($op == "activity_list") {
    $setting = array();
    $header = "";
    $classid = get_get("classid");
    $uid = get_get("uid"); // requestuid
    $s_uid = getSessionUid(); // currectuid
    if (! $uid)
        $uid = $s_uid;
    if ($uid != $s_uid) {
        if (! allowVisit($s_uid, $uid)) {
            printMessage("error", "你无权访问对方的家园");
            die();
        } else {
            $header = "TA的动态";
            $setting["uid"] = $uid;
        }
        if ($classid) {
            printMessage("error", "无效的请求，你无法在别人的空间访问班级动态");
            die();
        }
    } else {
        $setting["uid"] = $uid;
        if (! $classid) {
            $header = "我的动态";
            $setting["isclass"] = false;
        } else {
            $classData = getClass($classid);
            if (! $classData) {
                printMessage("error", "你请求的 $classid 班级id无效，无法找到对应的班级");
                die();
            }
            $displayname = $classData["displayname"];
            $header = "班级动态($displayname)";
            $setting["isclass"] = true;
            $setting["classid"] = $classid;
        }
    }
    $setting["header"] = $header;
    setSetting("activity_list", $setting);
    loadTemp("home/zone_view/activity_list.php");
    die();
}
if ($op == "detial_profile") {
    $uid = get_get("uid");
    if (! $uid) {
        $uid = getSessionUid();
    }
    $s_uid = getSessionUid();
    $isOwner = $s_uid == $uid;
    $profile = getProfile($uid);
    if (! $profile) {
        printMessage("error", "无效的请求，该主人暂未完成基本资料的填写，无法读取其详细资料页面");
        die();
    }
    $classData = getClassdata($uid);
    $loginData = getLogindata($uid);
    $homeSetting = getHomeSetting($uid);
    $ds = $homeSetting["display_in_profile_card"];
    if ((! isAdmin($uid, $s_uid))) {
        if (! in_array("mail", $ds))
            $loginData["mail"] = "保密";
        if (! in_array("phone", $ds))
            $loginData["phone"] = "保密";
    } else {
        $loginData = getAllLoginData($uid);
    }
    $setting = array(
        "classData" => $classData,
        "profile" => $profile,
        "loginData" => $loginData
    );
    setSetting("detial_profile", $setting);
    loadTemp("home/zone_view/detial_profile.php");
    die();
}
if ($op == "image_updater_insert") {
    loadTemp("system/image_updater_insert.php");
    die();
}
if ($op == "file_updater_insert") {
    loadTemp("system/file_updater_insert.php");
    die();
}
if ($op == "general_big_file_update_checkSize") {
    $size = get_post("size");
    $data = array();
    $data["op"] = "reject";
    if (! $size) {
        returnMessage(- 1, "你没有选择文件啊", $data);
    }
    if (! allowUpdateBigFile(getSessionUid())) {
        returnMessage(- 2, "你没权限上传大文件啊", $data);
    }
    if ($size > 9999999999) {
        returnMessage(- 3, "你这文件也忒大了吧", $data);
    }
    $data["op"] = "allow";
    returnMessage(0, "allow", $data);
}
if ($op == "general_big_file_update_header") {
    $size = get_post("size");
    $data = array();
    $data["op"] = "reject";
    if (! $size)
        returnMessage(- 1, "你没有选择11文件啊", $data);

    if (! allowUpdateBigFile(getSessionUid()))
        returnMessage(- 2, "你没权限上传大文件啊", $data);

    if ($size > 2 * 1024 * 1024 * 1024)
        returnMessage(- 3, "你这文件也忒大了吧", $data);

    $name = get_post("name");
    if (! $name)
        returnMessage(- 4, "文件名是啥", $data);
    $all = get_post("allamount");
    $uid = createBigFileHeader(substr($name, strrpos($name, '.') + 1), $all, $size);
    $data["op"] = "header_callback";
    $data["uid"] = $uid;

    returnMessage(0, "allow $all", $data);
}
if ($op == "general_big_file_update_cell") {
    $data = array();
    $data["op"] = "progress";
    $index = get_post("index");
    $uid = get_post("uid");
    if (! $uid)
        returnMessage(- 4, "你uid没跟着过来", $data);
    $file = $_FILES["data"]["tmp_name"];
    if (! $file)
        returnMessage(- 4, "你文件没跟着过来", $data);

    $orgianname = get_post("orgianname");
    if (! $orgianname)
        returnMessage(- 4, "你文件原始名没跟着过来", $data);
        
        $callback = receiveBigFileCell($file, $uid, $index,$orgianname);
    if ($callback["state"] == 1) {
        $data["op"] = "done";
        $data["data"] = $callback["data"];
    } else {
        $data["op"] = "updating";
        $data["now"] = $callback["nowamount"];
    }

    returnMessage(0, "接收成1功" . json_encode($_SESSION["s_bigFile"][$uid]), $data);
}
returnMessage("-2", "unknownrequest: $op");
?>